<?php
/**
 * Created by PhpStorm.
 * User: FDG296
 * Date: 2017/8/2
 * Time: 16:42
 */
namespace backend\models;

use Yii;
use yii\db\ActiveRecord;

class GoodsTagRelation extends ActiveRecord{
    const STATUS_ENABLE = 1;
    const STATUS_DISABLE = 0;

    public static function tableName(){
        return '{{%goods_tag_relation}}';
    }

    public function rules(){
        return [
            ['use_flg','default','value'=>self::STATUS_DISABLE],
            ['creator','getCreator'],
            ['create_time','date']
        ];
    }

    /**
     * @获取创建人
     */
    public function getCreator(){
        $userInfo = \common\helps\tools::getloginUserInfo();
        if($userInfo){
            $userName = $userInfo->userName;
        }else{
            $userName = '';
        }
        return $userName;
    }

    /**
     * 商品品类关联
     */
    public function addGoodsTagRelation($data,$tag_data){
        //获取品类组
        $data_now['tag_group_id'] = $tag_data['tag_group_id'];
        $data_now['tag_group_name'] = $tag_data['tag_group_name'];
        $data_now['tag_id'] = $tag_data['id'];
        $data_now['tag_name'] = $tag_data['tag_name'];
        $data_now['main_goods_id'] = $data['main_goods_id'];
        $data_now['sort'] = $data['sort'];
        $data_now['creator'] = $this->getCreator();
        $data_now['create_time'] = date('Y-m-d H:i:s',time());

        $red = Yii::$app->db->createCommand()->insert('j_goods_tag_relation',$data_now)->execute();
        return $red;
    }
}